#include "mymessagedialog.h"
#include "ui_mymessagedialog.h"
#include "common/tdebug.h"
#include <QMessageBox>
#include <QTimer>
#include "common/common.h"
#include <QDesktopWidget>
#include <QKeyEvent>


MyMessageDialog::MyMessageDialog(const QString &string,const QString &title,QWidget *parent) :
    TDialog(parent),
    ui(new Ui::MyMessageDialog)
{
    ui->setupUi(this);

//    this->setWindowTitle(title);

    ui->label->setText(string);
    ui->label->setWordWrap(true);
    ui->label_title->setText(title);
    connect(ui->btn_cancel,SIGNAL(clicked()),this,SLOT(clickCancel()));
    connect(ui->btn_confirm,SIGNAL(clicked()),this,SLOT(clickConfirm()));
    if(parent)
    {
        QSize size(WINDOW_SIZE(0.5,0.5));
        this->setGeometry((parent->width()-size.width())/2,(parent->height()-size.height())/2,size.width(),size.height());
    }
    else
    {
        this->setGeometry(WINDOW_GEOMETRY(0.5,0.5));
    }
    this->setFocus();

}


void MyMessageDialog::hideCancelBtn()
{
    ui->btn_cancel->hide();
}

void MyMessageDialog::hideConfirmBtn()
{
    ui->btn_confirm->hide();
}

void MyMessageDialog::hideBtn()
{
    ui->btnFrame->hide();
}

void MyMessageDialog::setTextForConfirmBtn(QString str)
{
    ui->btn_confirm->setText(str);
}
void MyMessageDialog::setTextForCancelBtn(QString str)
{
    ui->btn_cancel->setText(str);
}


int MyMessageDialog::question(QWidget *parent,
                    const QString &title,
                    const QString& text,
                    const QString& buttonConfirmText,
                    const QString& buttonCancelText)
{
    MyMessageDialog *dlg = new MyMessageDialog(text,title,parent);
    dlg->setTextForConfirmBtn(buttonConfirmText);
    dlg->setTextForCancelBtn(buttonCancelText);


    return dlg->exec();
}

void MyMessageDialog::warning(QWidget *parent,
                    const QString &title,
                    const QString& text,int msec)
{
    MyMessageDialog *dlg = new MyMessageDialog(text,title,parent);
    dlg->hideCancelBtn();
    if(msec>0)
    {
        dlg->timer.start(msec);
        connect(&(dlg->timer),SIGNAL(timeout()),dlg,SLOT(clickConfirm()));
    }
    dlg->exec();
}
void  MyMessageDialog::message(QWidget *parent,
                             const QString &title,
                             const QString& text,
                                          int msec)
{
     MyMessageDialog *dlg = new MyMessageDialog(text,title,parent);
     dlg->hideBtn();
     QTimer::singleShot(msec, dlg, SLOT(accept()));
     dlg->exec();

     //return dlg->exec();
}

MyMessageDialog * MyMessageDialog::messageShow(QWidget *parent,
                         const QString &title,
                         const QString& text)
{
    MyMessageDialog *dlg = new MyMessageDialog(text,title,parent);

    dlg->hideBtn();
    dlg->show();
    dlg->repaint();
    return dlg;
}

MyMessageDialog::~MyMessageDialog()
{
    ////tDebug()<<"delete MyMessageDialog";
    delete ui;
}
void MyMessageDialog::clickCancel()
{
    sendValue(RESULT_CANCEL);
    reject();
   //this->clearFocus();
//    this->close();
}
void MyMessageDialog::clickConfirm()
{
    sendValue(RESULT_OK);
    accept();
//    this->close();
}

#ifdef GRAB_SCREEN
#include <mainwindow.h>
#endif
void MyMessageDialog::keyPressEvent(QKeyEvent *keyEvent)
{
    if(keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return)
    {
        clickConfirm();
    }
    else if(keyEvent->key() == Qt::Key_Escape)
    {
        //tDebug("MyMessageDialog---Qt::Key_Escape");
        clickCancel();
    }
#ifdef GRAB_SCREEN
   else if(keyEvent->key()==KEY_ZERO)
   {
       MainWindow::grabScreen();
       return;
   }
#endif

}
